package com.chap11;

import java.util.concurrent.*;

/**
 * <p>
 * <code>ThreadPoolTest</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-04-03 15:22
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class ThreadPoolTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        ThreadPoolExecutor executor =
                new ThreadPoolExecutor(10,200,1000,
                        TimeUnit.SECONDS,new ArrayBlockingQueue<>(200));
//        executor.prestartAllCoreThreads();
        //往线程池提交任务
        executor.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println("...");
                throw  new NullPointerException();
            }
        });


        ArrayBlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(10);
        new Thread(new Runnable() {
            @Override
            public void run() {
                Runnable task ;
                while ((task = blockingQueue.poll())!=null){
                    //队列有任务，就可以获取到，然后执行任务
                    //队列是空的，线程获取任务的时候就会阻塞在这里
                    task.run();
                }
            }
        }).start();
    }
}
